/*
 * $Id: SessionBriefcase.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.service.support;

import com.sci.logisticsmap.service.model.GeodataDTModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The enumeration which provides an instance (singleton) for manipulating the session data.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
public enum SessionBriefcase {
    /**
     * The instance (singleton).
     */
    INSTANCE;

    /**
     * The logger.
     */
    private static final Logger logger = LoggerFactory.getLogger(SessionBriefcase.class);
    /**
     * The data map.
     */
    private Map<String, Entry> dataMap = new HashMap<String, Entry>();

    /**
     * Initializes the session data.
     *
     * @param session The http session.
     */
    public void init(HttpSession session) {
        dataMap.put(session.getId(), new Entry());
    }

    /**
     * Removes the session data associated with the given session.
     *
     * @param session The http session.
     */
    public void remove(HttpSession session) {
        dataMap.remove(session.getId());
    }

    /**
     * Clears the session data.
     */
    public void clear() {
        dataMap.clear();
    }

    /**
     * Adds the Geodata to the session data.
     *
     * @param session The http session.
     * @param geodata The Geodata.
     */
    public void addGeodata(HttpSession session, GeodataDTModel geodata) {
        Entry entry = dataMap.get(session.getId());
        entry.getGeodataList().add(geodata);
    }

    /**
     * The class <code>SessionBriefcase.Entry</code> represents the session data entry.
     */
    public class Entry {
        /**
         * The Geodata list.
         */
        private List<GeodataDTModel> geodataList;

        /**
         * The default constructor.
         */
        public Entry() {
            geodataList = new ArrayList<GeodataDTModel>();
        }

        /**
         * Gets the Geodata list.
         *
         * @return The Geodata list.
         */
        public List<GeodataDTModel> getGeodataList() {
            return geodataList;
        }
    }
}
